﻿Public Class xtf_LISTADOestadoCuotas
  Public WithEvents dtt_DataTable As New DataTable
    Public PERMISOImprimir As String = ""


#Region "Total registros ..."

    Private Sub gdv_general_RowCountChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gdv_general.RowCountChanged
        bsi_registros.Caption = "Total : " & gdv_general.RowCount
    End Sub

#End Region

#Region "General"
    Private Sub bbi_cerrar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_cerrar.ItemClick
        Me.Close()
    End Sub

    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        Me.Close()
    End Sub

    Private Sub bbi_actualizar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_actualizar.ItemClick
        dtt_DataTable = New DataTable
        IniciarLista()
    End Sub

    Private Sub xtf_Listado_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        guardar_config_usuariocolumna_()
    End Sub

    Private Sub guardar_config_usuariocolumna_()
        Dim obj_config_usuariocolumna_ As New cls_config_usuariocolumna_
        If obj_config_usuariocolumna_.Buscarxidusuarioxmodulo(UsuarioLOG.idusuario, Me.Name) Then
            obj_config_usuariocolumna_.xml = GetLayoutData(gdv_general)
            obj_config_usuariocolumna_.Modificar()
        Else
            obj_config_usuariocolumna_.xml = GetLayoutData(gdv_general)
            obj_config_usuariocolumna_.idusuario = UsuarioLOG.idusuario
            obj_config_usuariocolumna_.modulo = Me.Name
            obj_config_usuariocolumna_.Nuevo()
        End If
    End Sub

    Public Sub FiltrarColumnas()
        Dim int_IDUsuario As Integer = UsuarioLOG.idusuario
        Dim obj_config_usuariocolumna_ As New cls_config_usuariocolumna_
        gdv_general.BeginUpdate()
        If obj_config_usuariocolumna_.Buscarxidusuarioxmodulo(int_IDUsuario, Me.Name) Then
            SetLayoutData(gdv_general, obj_config_usuariocolumna_.xml)
        End If

        PerzonalizarControl(gdc_general)

        gdv_general.EndUpdate()
    End Sub

    Private Function GetLayoutData(ByRef prm_GridView As DevExpress.XtraGrid.Views.Grid.GridView) As Byte()
        Dim stream As New System.IO.MemoryStream()
        prm_GridView.SaveLayoutToStream(stream)
        Return stream.GetBuffer()
    End Function

    Private Sub SetLayoutData(ByRef prm_GridView As DevExpress.XtraGrid.Views.Grid.GridView, ByVal data As Byte())
        If data Is Nothing OrElse data.Length = 0 Then
            Return
        End If
        Dim stream As New System.IO.MemoryStream(data)
        Try
            prm_GridView.RestoreLayoutFromStream(stream)
        Catch ex As Exception
            Throw New Exception("Wrong data format", ex)
        End Try
    End Sub

    Private Sub bbi_columnas_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_columnas.ItemClick
        gdv_general.ColumnsCustomization()
    End Sub
#End Region

    Public Sub New()

        Me.Cursor = Cursors.WaitCursor
        InitializeComponent()
        PERMISOImprimir = "015005002"
        lbl_modulo.Text = "Consulta estado de Cuotas"
        Me.Text = lbl_modulo.Text : lbl_modulodescripcion.Text = "Listado de " & lbl_modulo.Text & "."
        IniciarLista()
        Me.Cursor = Cursors.Default

        dtp_fechaDesde.DateTime = Now

        Dim desde As DateTime = New Date(Now.Year, Now.Month, 1)
        Dim hasta As DateTime = DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, desde))
        dtp_fechaDesde.DateTime = desde
        dtp_fechaHasta.DateTime = hasta

    End Sub

    Public Sub IniciarLista()
        Try
            gdc_general.BeginUpdate()
            dtt_DataTable = New DataTable

            gdc_general.DataSource = dtt_DataTable
            'ulg_Lista.DisplayLayout.GroupByBox.Style = Infragistics.Win.UltraWinGrid.GroupByBoxStyle.Full
            'Es Pre            Póliza             Endoso                Cliente  Vigencia  Cuotas Obs                             Total  Cuota

            dtt_DataTable.Columns.Add("Identificador (ID)")
            dtt_DataTable.Columns.Add("Es pre cotización?")
            dtt_DataTable.Columns.Add("Póliza Tipo")
            dtt_DataTable.Columns.Add("Póliza Nro.")
            dtt_DataTable.Columns.Add("Endoso es endoso?")
            dtt_DataTable.Columns.Add("Endoso Nro.")
            dtt_DataTable.Columns.Add("Cliente")
            dtt_DataTable.Columns.Add("Vigencia")
            dtt_DataTable.Columns.Add("Cuotas")
            dtt_DataTable.Columns.Add("Obs.")
            dtt_DataTable.Columns.Add("Total En $")
            dtt_DataTable.Columns.Add("Cuota valor")

            'Dim obj_Personal_ As New cls_personal_
            'If obj_Personal_.CargarTodos() Then
            '    For Each Personal As cls_personal_ In obj_Personal_.Items
            '        AgregarItem(Personal)
            '    Next
            'End If
            gdv_general.PopulateColumns()
            FiltrarColumnas()
            gdv_general.BestFitColumns()
            gdc_general.EndUpdate()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub
    Public Sub AgregarItem(ByRef poliza As cls_poliza_)
        Dim Row As DataRow = dtt_DataTable.NewRow

        Row("Identificador (ID)") = poliza.idpoliza
        Row("Es pre cotización?") = IIf(poliza.precotizacion, "SI", "NO")
        Row("Póliza Tipo") = IIf(poliza.nueva, "Nueva", "Renov.")
        Row("Póliza Nro.") = poliza.numero
        Row("Endoso es endoso?") = IIf(poliza.endoso, "SI", "NO")
        If poliza.endoso Then Row("Endoso Nro.") = poliza.idpolizaendoso
        Row("Cliente") = poliza.cliente.ToString
        Row("Vigencia") = Format(poliza.vigenciadesde, "dd/MM") & " al " & Format(poliza.vigenciahasta, "dd/MM")
        Row("Cuotas") = poliza.cuotas
        Row("Obs.") = poliza.obs
        Row("Total En $") = poliza.importe
        Row("Cuota valor") = poliza.importecuota

        dtt_DataTable.Rows.Add(Row)
        'ulg_Lista.Rows(ulg_Lista.Rows.Count - 1) = Persona.Height = 19
    End Sub

    Public Sub btn_buscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_buscar.Click
        dtt_DataTable.Rows.Clear()
        Dim obj_poliza_ As New cls_poliza_
        Dim desde As DateTime = New Date(dtp_fechaDesde.DateTime.Year, dtp_fechaDesde.DateTime.Month, dtp_fechaDesde.DateTime.Day, 0, 0, 0)
        Dim hasta As DateTime = New Date(dtp_fechaHasta.DateTime.Year, dtp_fechaHasta.DateTime.Month, dtp_fechaHasta.DateTime.Day, 23, 59, 59)
        If obj_poliza_.CargarXvigencia(desde, hasta) Then
            For Each poliza As cls_poliza_ In obj_poliza_.Items
                Dim agregar As Boolean = False
                If chk_endoso.Checked And poliza.endoso Then
                    agregar = True 
                End If

                If chk_precotizacion.Checked And poliza.precotizacion Then
                    agregar = True
                End If

                If chk_nueva.Checked And poliza.nueva Then
                    agregar = True 
                End If

                If chk_renueva.Checked And Not poliza.nueva Then
                    agregar = True
                End If

                If chk_todas.Checked Then
                    agregar = True
                End If
                If agregar Then
                    AgregarItem(poliza)
                End If
            Next
        End If
    End Sub

    'Public Sub btn_limpiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    dtt_DataTable.Rows.Clear()
    '    txt_buscarAgrupacion.Text = ""
    '    txt_buscarCategoria.Text = ""
    '    txt_buscarContacto.Text = ""
    '    txt_buscarDireccion.Text = ""
    '    txt_buscarLocalidad.Text = ""
    '    txt_buscarNombre.Text = ""
    '    txt_buscarObservaciones.Text = ""
    '    txt_buscarOtros.Text = ""
    '    txt_buscarTelefono.Text = ""
    'End Sub

    
    Public Overridable Sub bbi_imprimir_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_imprimir.ItemClick
        If ComprobarPermiso(PERMISOImprimir) Then
            ShowGridPreview(lbl_modulo.Text, gdc_general, False)
        End If
    End Sub

    Public Overridable Sub bbi_exportarPDF_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_exportarPDF.ItemClick
        If ComprobarPermiso(PERMISOImprimir) Then
            ExportGrid(lbl_modulo.Text, gdc_general, "PDF")
        End If
    End Sub

    Public Overridable Sub bbi_vistaprevia_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_vistaprevia.ItemClick
        If ComprobarPermiso(PERMISOImprimir) Then
            'i.Detail.
            ShowGridPreview(lbl_modulo.Text, gdc_general, True)
        End If
    End Sub

    Public Overridable Sub bbi_exportarExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_exportarExcel.ItemClick
        If ComprobarPermiso(PERMISOImprimir) Then
            ExportGrid(lbl_modulo.Text, gdc_general, "XLS")
        End If
    End Sub


    Private Function generarReporte() As xtr_REPORTEcuota_

        Dim report As New xtr_REPORTEcuota_
        report.DataSource = gdc_general.DataSource
        If dtp_fechaDesde.DateTime.Month = dtp_fechaHasta.DateTime.Month And dtp_fechaDesde.DateTime.Year = dtp_fechaHasta.DateTime.Year Then
            report.lbl_informe.Text = "Informe de Cuotas : " & Format(dtp_fechaHasta.DateTime, "MMMM") & " " & dtp_fechaHasta.DateTime.Year & " (" & dtp_fechaDesde.DateTime.Day & " al " & dtp_fechaHasta.DateTime.Day & ")"
        Else
            report.lbl_informe.Text = "Informe de Cuotas : " & dtp_fechaDesde.DateTime.ToShortDateString & " al " & dtp_fechaHasta.DateTime.ToShortDateString
        End If
        Dim Total As Decimal
        For Each row As DataRow In dtt_DataTable.Rows
            Total += row("importe")
        Next
        report.lbl_totalimporte.Text = "Total Cuotas Listadas : " & gdv_general.RowCount
        report.lbl_totalcuotas.Text = "Importe Total : " & Math.Round(Total, 2)

        Return report
    End Function

    Private Sub chk_todas_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_todas.CheckedChanged
        chk_endoso.Enabled = Not chk_todas.Checked
        chk_endoso.Checked = chk_todas.Checked

        chk_precotizacion.Enabled = Not chk_todas.Checked
        chk_precotizacion.Checked = chk_todas.Checked

        chk_nueva.Enabled = Not chk_todas.Checked
        chk_nueva.Checked = chk_todas.Checked

        chk_renueva.Enabled = Not chk_todas.Checked
        chk_renueva.Checked = chk_todas.Checked
    End Sub

    Private Sub btn_exportarImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exportarImprimir.Click
        bbi_vistaprevia_ItemClick(Nothing, Nothing)
    End Sub
End Class